package com.phgame.newsmth.ui;

import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.android.bitmapfun.util.ImageWorker;
import com.phgame.newsmth.R;
import com.phgame.newsmth.task.GetUserInfoTask;
import com.phgame.newsmth.util.SharePropertyUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

public class UserInfoActivity extends BaseActivity {

	private TextView userNameText;
	private ImageView avatarView;
	private TextView nicknameText;
	private TextView sexText;
	private TextView regionText;
	private TextView signatureText;
	private TextView scoreText;
	private TextView statusText;
	private TextView lastloginText;


	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			String json = bundle.getString("json");
			try {
				JSONObject o = new JSONObject(json);
				userNameText.setText(o.getString("id"));
				nicknameText.setText(o.getString("user_name"));
				sexText.setText(o.getString("gender"));
				regionText.setText(convertIptoCity(o.getString("last_login_ip")));
				//signatureText.setText(o.getString())
				scoreText.setText(o.getString("score_user"));
				lastloginText.setText(new Date(Integer.parseInt(o.getString("last_login_time")) * 1000).toLocaleString());
				
				ImageWorker worker = ((MyApplication) (UserInfoActivity.this.getApplicationContext())).getImageWorker();
				worker.loadImage(o.getString("face_url"), avatarView);
				
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}

	};
	private String convertIptoCity(String ip){
		return null;
	}

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
/*		String json = "{\"id\":\"shcalm\",\"user_name\":\"shcalm\",\"face_url\":\"http://images.newsmth.net/nForum/uploadFace/S/shcalm.9812.jpg\",\"face_width\":120,\"face_height\":115,\"gender\":\"m\"}";
		try {
			JSONObject o = new JSONObject(json);
			String a = (String) o.get("id");
			String b = (String) o.get("face_url");
			System.out.println("====" + a + "=====" + b);
			JSONArray array = new JSONArray(json);
			a = array.getString(0);
			b = array.getString(1);
			System.out.println("====" + a + "=====" + b);
			array.getJSONObject(0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/

		this.setContentView(R.layout.userinfo);

		userNameText = (TextView) this.findViewById(R.id.username);
		avatarView = (ImageView) this.findViewById(R.id.avatar);
		nicknameText = (TextView) this.findViewById(R.id.nickname);
		sexText = (TextView) this.findViewById(R.id.sex);
		regionText = (TextView) this.findViewById(R.id.region);
		signatureText = (TextView) this.findViewById(R.id.signature);
		scoreText = (TextView) this.findViewById(R.id.score);
		statusText = (TextView) this.findViewById(R.id.status);
		lastloginText = (TextView) this.findViewById(R.id.lastlogintime);

		GetUserInfoTask task = new GetUserInfoTask(mHandler);
		SharePropertyUtil util = SharePropertyUtil.getInstance(this);
		String username = util.readProperty("username");
		if(username.length() != 0)
		    task.execute(username);

		// String json = "[*]";
	}
}
